package com.jxust.vhr.controller;

import cn.hutool.core.util.RandomUtil;
import com.jxust.debate.pojo.Cacode;
import com.jxust.debate.pojo.Company;
import com.jxust.debate.pojo.SystemAdministrator;
import com.jxust.debate.util.CommonResult;
import com.jxust.debate.util.ResultEnum;
import com.jxust.vhr.service.CompanyService;
import com.jxust.vhr.service.SysAdminService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(value = "系统管理员Controller",tags = "系统管理员操作接口")
@RestController
@RequestMapping("admin/SysAdmin")
public class SysAdminController {

    @Autowired
    private SysAdminService sysAdminService;

    @Autowired
    private CompanyService companyService;

    /**
     * 系统管理员登录
     * @param systemAdministrator 账号与密码信息
     * @return 返回登录信息 （密码置空）
     */
    @ApiOperation("系统管理员登录")
    @PostMapping("/Login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "saacount", value = "系统管理员账号", required = true),
            @ApiImplicitParam(name = "sapassword", value = "系统管理员密码", required = true)
    })
    public CommonResult<SystemAdministrator> Login(SystemAdministrator systemAdministrator){
        return sysAdminService.Login(systemAdministrator);
    }

    /**
     * 为公司管理员创建认证码
     * @param cid 传入公司ID
     * @return 返回创建的认证码（公司管理员认证码首两字母为CA）
     */
    @ApiOperation("为公司管理员创建认证码")
    @PostMapping("/CodeCreate")
    public CommonResult<Cacode> CodeCreate(@RequestParam @ApiParam(name = "cid",value = "公司管理员所属公司ID") String  cid){
        return sysAdminService.CodeCreate(cid);
    }

    /**
     * 注册公司
     * @param company 公司信息
     * @return 返回注册信息
     */
    @ApiOperation("注册公司")
    @PostMapping("/CompanyRegister")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cname", value = "公司名称", required = true),
            @ApiImplicitParam(name = "cprofile", value = "公司简介", required = true),
            @ApiImplicitParam(name = "caddress", value = "公司所在地址", required = true),
            @ApiImplicitParam(name = "ccontacts", value = "公司联系方式", required = true)
    })
    public CommonResult<Company> CompanyRegister(Company company){
        company.setCid(RandomUtil.randomString(32));
        company.setCregistration(new Date());
        CommonResult<Company> commonResult= companyService.CompanyRegister(company);
        if(commonResult.getCode()== ResultEnum.SUCCESS.getCode()){
            commonResult.setData(company);
        }
        return commonResult;
    }




    //return new CommonResult<>(101,"test meg");
}
